package com.workspaceapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ExecutionException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.globallogic.myworkspace.R;
//import com.globallogic.myworkspace.UserResourceActivity;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.jackson.JacksonFactory;
import com.globallogic.myworkspace.userendpoint.Userendpoint;
import com.globallogic.myworkspace.userendpoint.model.User;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	public class EndpointsTask extends AsyncTask<ArrayList<String>, Integer, Long> {
		
		protected Long doInBackground(ArrayList<String>... array) {
        	Userendpoint.Builder endpointBuilder = new Userendpoint.Builder(
  	              AndroidHttp.newCompatibleTransport(),
  	              new JacksonFactory(),
  	              new HttpRequestInitializer() {
  	              public void initialize(HttpRequest httpRequest) { }
  	              });
        	Userendpoint endpoint = endpointBuilder.build();
  	      try {
  	    	  String unm = array[0].get(0);
  	    	  String pwd = array[0].get(1);
  	    	  //endpoint call
  	          User user = endpoint.getAuthenticateUser(unm, pwd).execute();
  	          
  	        if (!user.containsKey("error_message"))
	          {
  	        	 return (long) 1;
  	        	//Intent activityChangeIntent = new Intent(MainActivity.this, ResourceActivity.class);
  	        	//MainActivity.this.startActivity(activityChangeIntent);
	          }
  	        else
  	          {
  	        	return (long) 0;
  	          }
  	          //String noteID = new Date().toString();
  	          //note.setId(noteID);

  	          ///note.setEmailAddress(notes[0].getEmailAddress());      
  	          //Notes result = endpoint.insertNotes(note).execute();
  	          //result.getDescription();
  	          
  	      } catch (IOException e) {
  	        e.printStackTrace();
  	      }
          
          return (long) 0;
        }
        
        
        protected void onPostExecute(Long result) {
        	TextView text = (TextView) findViewById(R.id.lblResponse);    		
    			if (result == 1)
    			{
    				super.onPostExecute(result);
    			Intent intent = new Intent(MainActivity.this,ResourceActivity.class);
    			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    			intent.putExtra("username", ((EditText)findViewById(R.id.editusername)).getText().toString());
    			getApplicationContext().startActivity(intent);
    		//	MainActivity.this.startActivity(activityChangeIntent)    
    				//text.setText("Login successful!!");  
    			}
    			else
    				text.setText("Login failed!!");
        	
        }
    }
	
	@SuppressWarnings("unchecked")
	public void getAuthentication(View view) {

		ArrayList<String> strArr = new ArrayList<String>();
		//User notes = new User();
		EditText txtusername = (EditText)findViewById(R.id.editusername);
		String username = txtusername.getText().toString();
		strArr.add(username);
		//notes.setDescription(descp);
		EditText txtpwd = (EditText)findViewById(R.id.editpassword);
		String password = txtpwd.getText().toString();
		strArr.add(password);
		//notes.setEmailAddress(Email);
		//PosttoCloud(descp,Email);
		new EndpointsTask().execute(strArr);		
		
	        
	}

}
